<?php 
/**************************************************/
## Class Name - Model_BLOGS (Contains all the functions related Blog modules)
## Created By - Techmodi (22/08/2011)
/**************************************************/
class Model_Blogs extends Database
{	
	function Model_Blogs() {	
		$this->Blogs = BLOGS;
		$this->BlogsComment = BLOGSCOMMENT;
		$this->user = USERS;
		$this->Database();
	}
	
	// for add Blog
	function addBlog($array) {
		$this->InsertData($this->Blogs, $array);
		return mysql_insert_id();
	}
	
	## List all Blogs
	function getAllBlogs($limit='',$offset='') {
		$fields = array("$this->Blogs.* ");	
		$tables = array($this->Blogs);
		$where = array(" status = '1'");
		$order = array(" id DESC ");
		$result1 = $this->SelectData($fields,$tables, $where , $order , $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		
		for($i=0;$i<count($result);$i++)
		{
			$result[$i]['url'] = 'blog-detail/'.base64_encode($result[$i]['id']);
		}
		return $result;
	}
	
	## Get Blog details by id
	function getBlogDetailsById($id) {
		$fields = array();	
		$tables = array($this->Blogs);
		$result1 = $this->SelectData($fields,$tables, $where= array("id=$id"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	function countBlogs() {
		$fields = array();	
		$tables = array($this->Blogs);
		$result1 = $this->SelectData(array(" count(id)"),$tables, $where= array("status='1'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result[0];
	}
	
	## Edit Blog by id
	function editBlogById($array, $Id){
		$this->UpdateData($this->Blogs,$array,"id",$Id,0);
	}
	
	## Update Blog status with multiple ids
	function updateBlogStatus($ids, $status) {
		$sql = "UPDATE ".$this->Blogs." SET status='".$status."' WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
		
	}
	
	## Delete Blog with multiple ids
	function deleteBlog($ids, $status) {
		$sql = "DELETE FROM ".$this->Blogs." WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Delete Blog  by id
	function deleteBlogById($id){
		$this->DeleteData($this->Blogs,"id",$id);
	}
	
	/*****************  Blog Comment Start   ***********************************/
	
	// for add Blog Comment
	function addBlogComment($array) {
		$this->InsertData($this->BlogsComment, $array);
		return mysql_insert_id();
	}
	
	## Get Blog Comment by Blog id
	function getAllBlogsComment($blog_id,$search,$col1='',$col2='',$limit='',$offset='') {
		$fields = array("$this->BlogsComment.*,$this->user.first_name,$this->user.last_name");	
		$tables = array($this->BlogsComment,$this->user);
		if(trim($search)) {
			$where[] = "comment LIKE '%".$search."%' and blog_id=$blog_id and $this->Blogs.user_id=$this->user.id";
		}
		else
		{
			$where[] = "blog_id=$blog_id and $this->BlogsComment.user_id=$this->user.id";
		}
		if($col1 =='0')
		{
			$order = array("first_name ASC");
		}
		if($col2 =='0')
		{
			$order = array("comment_date ASC");
		}		
		if($col1 =='1')
		{
			$order = array("first_name DESC");
		}
		if($col2 =='1')
		{
			$order = array("comment_date DESC");
		}
		
		$result1 = $this->SelectData($fields,$tables, $where , $order, $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## Get Blog Comment details by id
	function getBlogCommentDetailsById($id) {
		$fields = array();	
		$tables = array($this->BlogsComment);
		$result1 = $this->SelectData($fields,$tables, $where= array("id=$id"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	
	## Edit Blog Comment by id
	function editBlogCommentById($array, $Id){
		$this->UpdateData($this->BlogsComment,$array,"id",$Id,0);
	}
	
	## Update Blog  Comment status with multiple ids
	function updateBlogCommentStatus($ids, $status) {
		$sql = "UPDATE ".$this->BlogsComment." SET status='".$status."' WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
		
	}
	
	## Delete Blog Comment with multiple ids
	function deleteBlogComment($ids, $status) {
		$sql = "DELETE FROM ".$this->BlogsComment." WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Delete Blog Comment by id
	function deleteBlogCommentById($id){
		$this->DeleteData($this->BlogsComment,"id",$id);
	}
	
	## Get Blog Comment by Blog id
	function getCommentCountByBlogId($id) {
		$fields = array('count(*) as cnt');	
		$tables = array($this->BlogsComment);
		$result1 = $this->SelectData($fields,$tables, $where= array("blog_id=$id"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result['cnt'];
	}

	## fetch last 5 blog
	function getLastTwoBlog() {
		$fields=array();	
		$tables=array($this->Blogs);
		$where=array("");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("id DESC"), $group=array(),$limit = "2",0,0); 
		$result= $this->FetchAll($result1); 
		for($i=0;$i<count($result);$i++)
		{
			$result[$i]['url'] = 'blog-detail/'.base64_encode($result[$i]['id']);
		}
		return $result;	
	}
    public function stringURLSafe($string)
	{
		// Remove any '-' from the string since they will be used as concatenaters
		$str = str_replace('-', ' ', $string);

		// Trim white spaces at beginning and end of alias and make lowercase
		$str = trim(strtolower($str));

		// Remove any duplicate whitespace, and ensure all characters are alphanumeric
		$str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str);

		// Trim dashes at beginning and end of alias
		$str = trim($str, '-');

		return $str;
	}

 
}
?>


